package LeetCode.month01.day17;

import java.lang.management.MemoryType;

public class No152 {
    public static void main(String[] args) {
        System.out.println(maxProduct(new int[]{2,3,-2,4}));
    }
    public static int maxProduct(int[] nums) {
        int[] max = new int[nums.length];
        int[] min = new int[nums.length];
        for (int i = 0; i < nums.length; i++) {
            max[i] = nums[i];
            min[i] = nums[i];
        }
        for (int i = 1; i <nums.length; i++) {
            max[i] = Math.max(Math.max(max[i],nums[i]*max[i-1]),nums[i]*min[i-1]);
            min[i] = Math.min(Math.min(min[i],nums[i]*min[i-1]),nums[i]*max[i-1]);
        }
        int ans = nums[0];
        for (int i = 0; i < nums.length; i++) {
            ans = Math.max(max[i],ans);
        }
        return ans;
    }
}
